'use strict';

module.exports = app => {
    return class CollectService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 取消收藏
         * @param id
         * @returns {Promise<void>}
         */
        async cancel({ ids }) {
            let url = this.app.urls('collectionDelete');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    collectionIds: ids
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 添加收藏
         * type： 1.社区 2.帖子 3.问答
         * @param id
         * @param type
         * @returns {Promise<void>}
         */
        async add({ id, type }) {
            let url = this.app.urls('collectionAdd');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    domainId: id,
                    type
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 获取收藏列表
         * @param id
         * @param type
         * @returns {Promise<void>}
         */
        async list({ type, pageNo }) {
            let url = this.app.urls('collectionPost');

            if (type == 1){
                url = this.app.urls('collectionForum');
            }

            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    type,
                    pageNo
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;


        }


    }

};
